#!/bin/bash


# edit area
JAVA_HOME8=/opt/soft/java8/bin/java

JAVA_OPTS="-server -Xms512m -Xmx512m -Xmn128M -Xss256k \
-XX:+UseParallelGC -XX:ParallelGCThreads=16 -XX:+UseParallelOldGC \
-XX:MaxDirectMemorySize=512m"
APP_NAME="op-account-service"

FILE_PATH="$(dirname $(readlink -f $0))"

if [ ! -d "$FILE_PATH/logs" ]
then
   mkdir "$FILE_PATH/logs"
fi

LOG_PATH="/dev/null"

RUN_COMMAND="$JAVA_HOME8  $JAVA_OPTS -jar $APP_NAME.jar"
#end

PID_FILE="$FILE_PATH/logs/server.pid"
pid=''

cd $FILE_PATH
start(){
	if [ -e $PID_FILE ]; then  
	   pid=`cat $PID_FILE`
	   if [ "$pid" != '' ];then
		 kill -9 $pid
		 echo 'KILL PID:[ '$pid '] OK!'
	   fi
	fi
        echo 'current shell dir:'`pwd`
        nohup $RUN_COMMAND >$LOG_PATH 2>&1 & echo $! > $PID_FILE
        echo 'START SERVER OK!'
	    echo 'CURRENT PID IS:['`cat $PID_FILE`']'
}
stop(){
	if [ -e $PID_FILE ]; then  
	   pid=`cat $PID_FILE`
	   if [ "$pid" != '' ];then
		kill -9 $pid
        echo 'KILL PID:[ '$pid '] OK!'
        rm $PID_FILE
		echo "SERVER STOP OK!"
	   fi
	fi
}

case "$1" in 
start) 
start 
;; 
stop) 
stop 
;; 
*) 
printf 'Usage: {start|stop} to start or stop server! \n'
exit 1 
;; 
esac
